package com.msb.api.filter.impl;

import com.msb.api.feign.BeaconCacheClient;
import com.msb.api.filter.CheckFilter;
import com.msb.common.constart.CacheConstant;
import com.msb.common.enums.ExceptionEnums;
import com.msb.common.exception.ApiException;
import com.msb.common.exception.GraceException;
import com.msb.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: lmf
 * @Create: 2025/3/7 9:01
 * Description: apiKey校验
 */
@Slf4j
@Service("apiKey")
public class ApiKeyCheckFilter implements CheckFilter {
    @Resource
    private BeaconCacheClient cacheClient;

    @Override
    public void check(StandardSubmit standardSubmit) {
        log.info("校验ApiKeyCheckFilter");
        final Map<String, Object> clientBusiness =
                cacheClient.hGetAll(CacheConstant.CLIENT_BUSINESS + standardSubmit.getApiKye());

        // 没有查询到客户数据直接返回异常
        if (clientBusiness == null || clientBusiness.isEmpty()) {
            GraceException.display(ExceptionEnums.ERROR_APIKEY);
        }

        // 正常封装数据
        // 设置客户id和ip
        standardSubmit.setClientId(Long.parseLong(clientBusiness.get("id") + ""));
        standardSubmit.setIp(clientBusiness.get("ipAddress") + "");

        log.info("【通过apiKey】验证后的信息：{}", standardSubmit);
    }
}
